﻿using System;
using Microsoft.SharePoint;

namespace SPRepository
{
    public class SimpleFieldType<T> : IFieldType<T> where T : class
    {
        private readonly Guid _fieldId;

        public SimpleFieldType(Guid fieldId)
        {
            _fieldId = fieldId;
        }

        public Guid FieldId
        {
            get { return _fieldId; }
        }

        public virtual T GetValue(SPListItem item)
        {
            return GetFieldValue<T>(item);
        }

        public virtual void SetValue(SPListItem item, T value)
        {
            SetFieldValue(item, value);
        }

        protected TF GetField<TF>(SPListItem item) where TF : SPField
        {
            return item.Fields[FieldId] as TF;
        }

        protected TF GetFieldValue<TF>(SPListItem item) where TF : class
        {
            return item[FieldId] as TF;
        }

        protected void SetFieldValue(SPListItem item, object value)
        {
            item[FieldId] = value;
        }

        protected object GetFieldRawValue(SPListItem item)
        {
            return item[FieldId];
        }
    }
}